Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
How to "append" an action to a function?

I wanted to create a function that I can add actions to later. I tried this:

Function.prototype.appendAction = function(action) {
  let _ogFn = this
  return function(...args) {
    _ogFn(...args)
    action()
  }
}
function a() {
  console.log("foo")
}
a()
a = a.appendAction(() => console.log("bar"))
a()

This does work, but how can I make it change the function automatically? I want to make it work like this:

a()
a.appendAction(() => console.log("bar")) //note it doesn't have the "a = "
a() //changed function
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Depending on your scenario you could create a function decorator. This would mean defining a function (for example fnMod) that accepts a function fn and returns a decorated version that executes some before and after hooks.

const a = fnMod(() => {
  console.log("foo");
});

a();
a.appendAction(() => console.log("bar"));
a();



const john = {
  name: "John Doe",
  age: 42,
  greet: fnMod(function () {
    console.log(`I'm ${this.name}.`); // <- uses `this` so use a normal function
  })
}

john.greet.prependAction((name) => {
  console.log(`Hi ${name}!`); // <- does not use `this` so both a normal function
});                           //    or arrow function will work fine

john.greet.appendAction(function () {
  console.log(`I'm ${this.age} years old.`);
});

john.greet("Jane");



function fnMod(fn) {
  const before = [];
  const after = [];
  
  function hookable(...args) {
    before.forEach(fn => fn.apply(this, args));
    const result = fn.apply(this, args);
    after.forEach(fn => fn.apply(this, args));
    return result;
  };
  
  hookable.prependAction = (fn) => {
    before.unshift(fn);
  }
  
  hookable.appendAction = (fn) => {
    after.push(fn);
  }
  
  return hookable;
}

You might want to tweak it to your liking. Currently both this and all arguments passed to the decorated function are forwarded to all hooks, which might or might not be desirable.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you just want to call it immediately:

Function.prototype.appendAction = function(action) {
  let _ogFn = this
  return (function(...args) {
    _ogFn(...args)
    action()
  })()
}
function a() {
  console.log("foo")
}
a.appendAction(() => console.log("bar"))

That is the immediately invoked function expression (IIFE) pattern

about 4 years ago · Juan Pablo Isaza Denunciar

0

For your purposes you could use eval:

First, grab the user input however you are handling it and parse it as a string.

Then use eval like so:

let userInput = 'console.log("hello world")';

const callUserFunction = (action) => {
  eval(`function fn(){${action}}`);
  fn();
}

callUserFunction(userInput);

Be aware that allowing a user to write or JS is a security risk, which can open doors to all kinds of attacks, so in most scenarios, I would discourage doing so

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda